var stringName = "";
var batteryDayList = [];
var batteryListAll = [];
var batteryNameList = [];
var batteryTimeList = [];
var saveUpdateFlag = "";
var tableOrDivFlag = "";
var saveUpdateIndex;
var totalPage = 0;
var totalCnt = 0;

$(document).ready(function() {
	showLoading();
	$('title', parent.document).html($('title').text());
	createTableHead("batteryAllThead", batteryAllTable, seeAll);
	setBaseUrl();
	search();
	setTimeout(hideLoading, 500);
});

/*查询每天的统计并赋予click事件*/
function search(){
	$.ajax({
		type : 'post',
		url : 'searchBatteryTime',
		dataType : 'json',
		// data : $("#batteryTypeSearch").serialize(),
		success : function(data) {
			batteryTimeList = data && data.batteryTimeList || [];
			var length = batteryTimeList.length;
			if (length == 0) {
				showMessge(messageContent["Search.NoResultFound"]);
			} else {
				$("#allBatteryMsg").html('');
				var ul = "<ul class='leftAll_Ul'>" ;
				for(var i=0; i<length; i++) {
					ul += '<li class="leftClickLi leftClick" id="date'+ i + '" onclick="searchName(\'date'+ i + '\', '+ i + ')">'
						+ date2StringWord(batteryTimeList[i].createDTTM)
						+ '<span class="fontValue">'
						+ '<img id="selectRightPng" class="selectRightPng" src="../static/images/selectRightPng.png" width="12" height="12"/>'
						+ '<img id="selectBottomPng" class="selectBottomPng" src="../static/images/selectBottomPng.png" width="12" height="12" hidden="hidden"/>'
						+ '</span></li>'
						+ '<li class="leftClickLi showOrHide" hidden="hidden">'
						+ '<ul class="leftClickLiUl" id="date'+ i + 'Name">'
						+ '</ul></li>'
				}
				ul += "</ul>" ;
				$("#allBatteryMsg").append(ul);
				$(".leftClick").each(function(){
					$(this).click(function(){
				//			 $("#roleID").val($(this)[0].id);
						if (stringName != $(this)[0].innerText) {
							$(".showOrHide").hide();
							$('.leftClick').find('img#selectRightPng').show();
							$('.leftClick').find('img#selectBottomPng').hide();
						}
						$(this).next().toggle();
						$(this).find('img#selectRightPng').toggle();
						$(this).find('img#selectBottomPng').toggle();
						stringName= $(this)[0].innerText;
					});
				});
			}
		},
		error : function(data) {
			setTimeout(hideLoading, 500);
			if (data.status == "403" && data.statusText == "Forbidden") {
				showMessge(messageContent["Access.Forbidden"]);
			} else {
				showMessge(messageContent["Search.Error"]);
			}
		},
		timeout: function(data){
			setTimeout(hideLoading, 500);
			showMessge(messageContent["Access.Timeout"]);
		}
	});
}

/*根据时间查名字*/
function searchName(ID, num){
	$("#" + ID + "Name").html('');
	if (batteryTimeList[num].createDTTM && batteryTimeList[num].createDTTM) {
		$.ajax({
			type : 'post',
			url : 'searchBatteryDay',
			dataType : 'json',
			data : {
				CreateAlertDTTM : date2String(batteryTimeList[num].createDTTM) + ' 00:00:00',
				CreateAfterDTTM : date2String(batteryTimeList[num].createDTTM) + ' 23:59:59',
			},
			success : function(data) {
				batteryDayList = data && data.batteryDayList || [];
				var length = batteryDayList.length;
                tableOrDivFlag = "div";
				if (length == 0) {
					showMessge(messageContent["Search.NoResultFound"]);
				} else {
					for(var i=0; i<length; i++) {
						if(i<5) {
							var li =  '<li onclick="searchBatteryName('+ i +')"><img src="../static/images/batteryBluePng.png"/> ' + batteryDayList[i].batteryName + '</li>';
							$("#" + ID + "Name").append(li);
						}
					}
				}
			},
			error : function(data) {
				if (data.status == "403" && data.statusText == "Forbidden") {
					showMessge(messageContent["Access.Forbidden"]);
				} else {
					showMessge(messageContent["Search.Error"]);
				}
			},
			timeout: function(data){
				showMessge(messageContent["Access.Timeout"]);
			}
		});
	}

}

/*根据名字查内容*/
function searchBatteryName(num){
	saveUpdateIndex = num;
	showLoading();
	if (batteryDayList[num].batteryID) {
		$.ajax({
			type : 'post',
			url : 'searchBatteryName',
			dataType : 'json',
			data : {
				BatteryID : batteryDayList[num].batteryID
			},
			success : function(data) {
				batteryNameList = data && data.batteryNameList || [];
				var length = batteryNameList.length;
				if (length == 0) {
					showMessge(messageContent["Search.NoResultFound"]);
				} else {
					$("#rightDivTitle").html('');
					$(".divHideShow").hide();
					$("#rightDivTitle").html(batteryNameList[0].batteryName || '-');
					$("#updateDel").show();
					$("#MaxV").html(batteryNameList[0].maxVoltage || '-');
					$("#MaxC").html(batteryNameList[0].maxChargingCurrent || '-');
					$("#MaxOne").html(batteryNameList[0].maxOneVoltage || '-');
					$("#MinV").html(batteryNameList[0].minVoltage || '-');
					$("#MaxD").html(batteryNameList[0].maxDischargeCurrent || '-');
					$("#MinOne").html(batteryNameList[0].minOneVoltage || '-');
					$("#searchMessage").show();
				}
				setTimeout(hideLoading, 500);
			},
			error : function(data) {
				setTimeout(hideLoading, 500);
				if (data.status == "403" && data.statusText == "Forbidden") {
					showMessge(messageContent["Access.Forbidden"]);
				} else {
					showMessge(messageContent["Search.Error"]);
				}
			},
			timeout: function(data){
				setTimeout(hideLoading, 500);
				showMessge(messageContent["Access.Timeout"]);
			}
		});
	}
}
/*table内容*/
function seeAll(){
	$ajax('searchBatteryAll', 'searchBattery', searchBatteryTbody);
}
function searchBatteryTbody(data){
    tableOrDivFlag = "table";
	totalPage = data && data.totalPage || 0;
	totalCnt = data && data.totalCnt || 0;
	batteryListAll = data && data.batteryListAll || [];
	$("#rightDivTitle").html('');
	$("#rightDivTitle").html('所有电池');
	$(".divHideShow").hide();
	createTableBody("batteryAllTbody", batteryAllTable, batteryListAll);
    $(".showOrHide").hide();
    $('.leftClick').find('img#selectRightPng').show();
    $('.leftClick').find('img#selectBottomPng').hide();
	$("#searchAll").show();
}

/*添加*/
function save(){
	$("#rightDivTitle").html('电池添加');
	$(".divHideShow").hide();
	formReset("operationForm");
	$("#sureCancle").show();
	$("#saveUpdate").show();
	saveUpdateFlag = "save";
	$("#BatteryID").val(0);
}
/*修改*/
function update(index){
	if (!index) {
		index = 0;
	}
	$("#rightDivTitle").html('电池修改');
	$("#BatteryID").val(batteryNameList[0].batteryID || 0);
	$("#BatteryName").val(batteryNameList[0].batteryName || '-')
	$("#MaxVoltage").val(batteryNameList[0].maxVoltage || 0);
	$("#MinVoltage").val(batteryNameList[0].maxChargingCurrent || 0);
	$("#MaxChargingCurrent").val(batteryNameList[0].maxOneVoltage || 0);
	$("#MaxDischargeCurrent").val(batteryNameList[0].minVoltage || 0);
	$("#MaxOneVoltage").val(batteryNameList[0].maxDischargeCurrent || 0);
	$("#MinOneVoltage").val(batteryNameList[0].minOneVoltage || 0);
	$(".divHideShow").hide();
	$("#sureCancle").show();
	$("#saveUpdate").show();
	saveUpdateFlag = "update";
}
function getUrl(){
    return 'deleteBattery';
}
function getData(){
    if (tableOrDivFlag == "div") {
        return {BatteryIDList: (batteryNameList[0].batteryID).toString()};
    } else if (tableOrDivFlag == "table"){
        return {BatteryIDList: getCheckDataList(batteryListAll, "batteryID")};
    }
}
function sure(data){
    var result = data && data.result;
    if (result == "Success") {
        showMessge(messageContent["Delete.Success"]);
        seeAll();
    } else if (result == "NotFound") {
        showMessge(messageContent["Delete.NotFound"]);
    } else {
        showMessge(messageContent["Delete.Error"]);
    }
}
/*添加/修改 确认*/
function saveOrUpdate(){
	if (checkForm("operationForm", batteryCheck)) {
		if (saveUpdateFlag == "save") {
			$.ajax({
				type : 'post',
				url : 'saveBattery',
				dataType : 'json',
				data : $("#operationForm").serialize(),
				success : function(data) {
					showMessge(messageContent["Insert.Success"]);
					seeAll();
				},
				error : function(data) {
					if (data.status == "403" && data.statusText == "Forbidden") {
						showMessge("您没有操作权限!");
					} else {
						showMessge(messageContent["Insert.Failed"]);
					}
				},
				timeout: function(data){
					showMessge(messageContent["Access.Timeout"]);
				}
			});
		} else if (saveUpdateFlag == "update"){
			$.ajax({
				type : 'post',
				url : 'updateBattery',
				dataType : 'json',
				data : $("#operationForm").serialize(),
				success : function(data) {
					showMessge(messageContent["Update.Success"]);
					seeAll();
				},
				error : function(data) {
					if (data.status == "403" && data.statusText == "Forbidden") {
						showMessge(messageContent["Access.Forbidden"]);
					} else {
						showMessge(messageContent["Update.Failed"]);
					}
				},
				timeout: function(data){
					showMessge(messageContent["Access.Timeout"]);
				}
			});
		}
	}
}

